

import javax.swing.*;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;

/**
 * @author 绛河
 * 学生功能页
 */
public class FunctionStudent {
    DatabaseConnection dc = new DatabaseConnection();
    Font font = new Font("仿宋", Font.BOLD, 20);
    JFrame page1;
    JLabel textIcon1;
    JTextField entryField2;
    JButton button1;
    JScrollPane scrollPane1;
    JTable tabulation1;
    JTableHeader header1;
    public void student() {
        page1 = new JFrame("学生管理系统");
        //设置窗体大小
        page1.setSize(950, 550);
        //设置窗口每次启动都显示在屏幕正中央
        page1.setLocationRelativeTo(null);
        page1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        page1.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        textIcon1 = new JLabel("课程名：");
        textIcon1.setBounds(230, 10, 120, 40);
        textIcon1.setFont(font);
        entryField2 = new JTextField(null,20);
        entryField2.setBounds(350,10,180,40);
        scrollPane1 = new JScrollPane();
        scrollPane1.setBounds(60, 80, 780, 400);
        button1 = new JButton("查询");
        button1.setBounds(600, 10, 80, 40);
        button1.setFont(font);
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Connection con = null;
                ResultSet rs = null;
                PreparedStatement ps;
                try {
                    con = dc.getConnection();
                    String sql = "select * from grade";
                    ps = con.prepareStatement(sql);
                    rs = ps.executeQuery();
                    int count = 0, count1 = 0;
                    while (rs.next()){
                        if(entryField2.getText()!=null
                            &&rs.getString("student_number").equals(Register.getLoginInformation())
                            &&entryField2.getText().equals(rs.getString("course_name"))){
                            count++;
                        }
                        if(rs.getString("student_number").equals(Register.getLoginInformation())){
                            count1++;
                        }
                    }
                    if(count==0&&count1==0){
                        JOptionPane.showMessageDialog(null, "没有指定的数据项");
                        entryField2.setText(null);
                        con.close();
                    }
                    if(count!=0){
                        System.out.println(count);
                        rs = ps.executeQuery();
                        Object[][] info = new Object[count][6];
                        count = 0;
                        while (rs.next()) {
                            if (rs.getString("course_name").equals(entryField2.getText()) && rs.getString("student_number").equals(Register.getLoginInformation())) {
                                info[count][0] = rs.getString("student_number");
                                info[count][1] = rs.getString("student_name");
                                info[count][2] = rs.getString("course_number");
                                info[count][3] = rs.getString("course_name");
                                info[count][4] = rs.getString("student_class");
                                info[count][5] = rs.getFloat("score");
                                count++;
                            }
                        }
                        String[] title = {"学号", "姓名", "课程号", "课程名", "班级", "成绩"};
                        tabulation1 = new JTable(info, title);
                        header1 = tabulation1.getTableHeader();
                        scrollPane1.getViewport().add(tabulation1);
                        tabulation1.getTableHeader().setPreferredSize(new Dimension(1, 30));
                        tabulation1.getTableHeader().setFont(font);
                        tabulation1.setRowHeight(40);
                        tabulation1.setFont(new Font("Dialog", 0, 19));
                    }
                    if (count1!=0&&count==0){
                        rs = ps.executeQuery();
                        Object[][] info = new Object[count1][6];
                        count1 = 0;
                        while (rs.next()) {
                            if (rs.getString("student_number").equals(Register.getLoginInformation())) {
                                info[count1][0] = rs.getString("student_number");
                                info[count1][1] = rs.getString("student_name");
                                info[count1][2] = rs.getString("course_number");
                                info[count1][3] = rs.getString("course_name");
                                info[count1][4] = rs.getString("student_class");
                                info[count1][5] = rs.getFloat("score");
                                count1++;
                            }
                        }
                        String[] title = {"学号", "姓名", "课程号", "课程名", "班级", "成绩"};
                        tabulation1 = new JTable(info, title);
                        header1 = tabulation1.getTableHeader();
                        scrollPane1.getViewport().add(tabulation1);
                        tabulation1.getTableHeader().setPreferredSize(new Dimension(1, 30));
                        tabulation1.getTableHeader().setFont(font);
                        tabulation1.setRowHeight(40);
                        tabulation1.setFont(new Font("Dialog", 0, 19));
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } finally {
                    if (con != null) {
                        try {
                            con.close();
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                }
            }

        });
        //将这些按钮和文本等加入到窗体中
        page1.add(textIcon1);
        page1.add(button1);
        page1.add(entryField2);
        page1.add(scrollPane1);
        page1.setVisible(true);//让组件显示
    }
}